﻿@page "/General/UserDefinedTypes"
@using LiveChartsCore.SkiaSharpView.Blazor
@using LiveChartsCore;
@using LiveChartsCore.Kernel;
@using LiveChartsCore.SkiaSharpView;

<CartesianChart
    Series="@series">
</CartesianChart>

@code {
    public class City
    {
        public string Name { get; set; } = string.Empty;
        public double Population { get; set; }
    }

    static UserDefinedTypes()
    {
        LiveCharts.Configure(config =>
            config.HasMap<City>((city, index) => new(index, city.Population)));
    }

    private City[] values = new City[]
    {
        new City { Name = "Tokyo", Population = 4 },
        new City { Name = "New York", Population = 6 },
        new City { Name = "Seoul", Population = 2 },
        new City { Name = "Moscow", Population = 8 },
        new City { Name = "Shanghai", Population = 3 },
        new City { Name = "Guadalajara", Population = 4 }
    };

    private static string TooltipFormatter(ChartPoint point)
    {
        var city = (City)point.Context.DataSource!;
        return $"{city.Population}M people in {city.Name}";
    }

    private ISeries[] series = new ISeries[]
    {
        new LineSeries<City>
        {
            Name = "Population",
            Values = new City[]
            {
                new City { Name = "Tokyo", Population = 4 },
                new City { Name = "New York", Population = 6 },
                new City { Name = "Seoul", Population = 2 },
                new City { Name = "Moscow", Population = 8 },
                new City { Name = "Shanghai", Population = 3 },
                new City { Name = "Guadalajara", Population = 4 }
            },
            YToolTipLabelFormatter = TooltipFormatter
        }
    };
}
